<template>
  <div class="ota-performance-chart-container">
    <div class="ota-performance-chart-title">OTAs</div>
    <!-- 图表部分 -->
    <div class="ota-performance-chart">
      <PieChart
        :option="optionAnnular"
        :width="'1.4444rem'"
        :height="'230px'"
      ></PieChart>
    </div>

    <!-- 图表详情部分（完全不变） -->
    <div class="chart-details">
      <div class="separator"></div>
      <div class="keywords-title">Keywords</div>
      <div class="keyword-list">
        <div class="keyword-item">Discounts /<span>156 reviews</span></div>
        <div class="keyword-item">
          Temperature control accurate /<span>89 reviews</span>
        </div>
        <div class="keyword-item">
          Aesthetic appeal /<span>123 reviews</span>
        </div>
        <div class="keyword-item">
          Promotions & Membership Benefits /<span>74 reviews</span>
        </div>
        <!-- <div class="keyword-item">Cleanliness /<span>68 reviews</span></div> -->
      </div>
      <div class="view-all" @click="handleClickTitle">View All</div>
    </div>
    <div v-if="isPopupVisible" class="popup-container">
      <div class="mask" @click="closePopup"></div>
      <div class="popup-content">
        <KeyWords :keywordData="directKeywordsData" @close="closePopup" />
      </div>
    </div>
  </div>
</template>

<script>
import { fontChart } from "@/utils/echartPxToRem";
import PieChart from "@components/common/PieChart.vue";
import KeyWords from "./KeyWords.vue";
export default {
  components: {
    PieChart,
    KeyWords,
  },
  data() {
    return {
      isPopupVisible: false, // 弹出框状态（不变）
      directKeywordsData: {
        title: "Direct Booking Keywords",
        totalReviews: 920, // 修正为实际总和
        keywords: [
          // 各条目数据保持不变
          {
            title: "Bathroom cleanliness",
            score: 6.4,
            reviewCount: 160,
            sentiment: "3 negative",
          },
          {
            title: "City views",
            score: 9.0,
            reviewCount: 210,
            sentiment: "14 positive",
          },
          {
            title: "Staff friendliness",
            score: 7.6,
            reviewCount: 250,
            sentiment: "6 positive",
          },
          {
            title: "Bar ambiance",
            score: 8.2,
            reviewCount: 180,
            sentiment: "10 positive",
          },
          {
            title: "Room service delays",
            score: 5.2,
            reviewCount: 120,
            sentiment: "4 negative",
          },
        ],
      },
    };
  },
  // 计算属性：只动态生成标题文本，其他配置完全保留
  computed: {
    optionAnnular() {
      return {
        tooltip: {
          trigger: "item",
        },
        title: {
          // 仅修改这里：动态拼接 totalReviews 的值
          text: `{line1|442}\n{line2|Reviews}`,
          left: "48%", // 原有定位不变
          top: "center",
          textAlign: "center",
          verticalAlign: "middle",
          textStyle: {
            rich: {
              line1: {
                fontSize: fontChart(16),
                color: "#333",
                fontWeight: 600,
              },
              line2: {
                fontSize: fontChart(12),
                color: "#333",
                fontWeight: 400,
              },
            },
          },
        },
        series: [
          {
            name: "Access From",
            type: "pie",
            center: ["50%", "50%"],
            radius: ["40%", "100%"],
            grid: {
              top: 0,
              left: 0,
              right: 0,
              bottom: 0,
            },
            avoidLabelOverlap: false,
            itemStyle: {
              borderWidth: 2,
              borderColor: "#fff",
            },
            label: {
              fontSize: fontChart(14),
              show: true,
              position: "inside",
              formatter: function (params) {
                const percent = params.percent;
                const name = params.name;
                return `{icon_${name}|}\n{percentStyle|${percent}%}`;
              },
              distance: 5,
              alignTo: "labelLine",
              bleedMargin: 5,
              padding: [0, 0, 0, 0],
              rich: {
                icon_Booking: {
                  backgroundColor: {
                    image: require("@/assets/platforms/booking.png"),
                  },
                  width: 16,
                  height: 16,
                },
                icon_Trip: {
                  backgroundColor: {
                    image: require("@/assets/platforms/trip.png"),
                  },
                  width: 16,
                  height: 16,
                },
                icon_Expedia: {
                  backgroundColor: {
                    image: require("@/assets/platforms/expedia.png"),
                  },
                  width: 16,
                  height: 16,
                },
                percentStyle: {
                  padding: [1, 2],
                  width: 25,
                  height: 25,
                  align: "center",
                  verticalAlign: "middle",
                  fontSize: fontChart(10),
                  color: "#fff",
                },
              },
            },
            emphasis: {
              scale: false,
              label: {
                show: true,
                fontSize: 40,
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: [
              {
                value: 300,
                name: "Booking",
                itemStyle: { color: "#0E3C8A" },
              },
              {
                value: 150,
                name: "Trip",
                itemStyle: { color: "#1755B4" },
              },
              {
                value: 200,
                name: "Expedia",
                itemStyle: { color: "#2A76C9" },
              },
            ],
          },
        ],
      };
    },
  },
  methods: {
    handleClickTitle(title, content) {
      this.isPopupVisible = true;
    },
    closePopup() {
      this.isPopupVisible = false;
    },
  },
};
</script>

<style scoped lang="scss">
/* 所有样式完全不变 */
.popup-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;

  .mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 1;
  }

  .popup-content {
    position: relative;
    z-index: 2;
    width: 100%;
    max-width: 733px;
    background-color: white;
    border-radius: 10px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
    overflow: hidden;
  }
}
.popup-content {
  margin-bottom: 10px;
}

.mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 9;
}
.ota-performance-chart-container {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 24px;
  border-radius: 16px;
  .ota-performance-chart-title {
    font-size:.125rem;
    font-weight: 700;
    text-align: center;
  }
  .ota-performance-chart {
    display: flex;
    flex: 1;
    align-items: center;
    justify-content: center;
    height: 100%;
  }
  .chart-details {
    .separator {
      height: 1px;
      background-color: #b0aaaa;
      margin-bottom: 8px;
    }
    .keywords-title {
      margin-bottom: 8px;
      font-size: .0972rem;
      font-weight: 600;
      color: #232323;
      text-align: center;
    }

    .keyword-list {
      display: flex;
      flex-direction: column;
      font-size: .0833rem;
      text-align: start;

      .keyword-item {
        margin-bottom: 8px;
        font-size: .0694rem;
        font-weight: 400;
        color: #6a6969;
        span {
          color: #232323;
          font-size: .0694rem;
          font-weight: 600;
          margin-left: 4px;
        }
      }
    }
    .view-all {
      text-align: center;
      color: #4199e1;
      font-size: .0694rem;
      font-weight: 600;

    }
    ::v-deep .view-all:hover {
      opacity: 0.7;
      cursor: pointer;
    }
  }
}
</style>
